﻿#region [AccelerometerService Using Statements]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.ServiceModel.Activation;
using AccelerometerServer.Interfaces;
#endregion

namespace AccelerometerServer.Services
{
    #region [AccelerometerService Structs]
    /// <summary>
    /// Struct used for returning data on the 3 axis
    /// </summary>
    public struct XYZData
    {
        public double X;
        public double Y;
        public double Z;
    }
    #endregion

    /// <summary>
    /// Enables services to store and retrieve information from the Windows Phone 7 
    /// Accelerometer XNA Simulator
    /// </summary>
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class AccelerometerService : IAccelerometerService
    {
        #region [AccelerometerService Properties]
        //static means it will only existe one reference for this fields meaning 
        //we can save the values from service invocation to service invocation

        static double AccelerationX { get; set; }
        static double AccelerationY { get; set; }
        static double AccelerationZ { get; set; }

        static double RotationX { get; set; }
        static double RotationY { get; set; }
        static double RotationZ { get; set; }

        static bool XNAStarted { get; set; }
        #endregion

        #region [AccelerometerService Interface Methods]
        /// <summary>
        /// Retrieves information about the current acceleration 
        /// applied to the Windows Phone 7
        /// </summary>
        /// <returns>Acceleration on all Axis</returns>
        public XYZData DownloadAccelData()
        {
            XYZData accelData;
            accelData.X = AccelerationX;
            accelData.Y = AccelerationY;
            accelData.Z = AccelerationZ;

            return accelData;
        }

        /// <summary>
        /// Retrieves information about the current rotation 
        /// applied to the Windows Phone 7
        /// </summary>
        /// <returns>Rotation on all Axis</returns>
        public XYZData DownloadRotationData()
        {
            XYZData rotData;
            rotData.X = RotationX;
            rotData.Y = RotationY;
            rotData.Z = RotationZ;

            return rotData;
        }

        /// <summary>
        /// Stores information on the server relative to the
        /// Windows Phone 7 acceleration
        /// </summary>
        /// <param name="x">Acceleration on the X axis</param>
        /// <param name="y">Acceleration on the Y axis</param>
        /// <param name="z">Acceleration on the Z axis</param>
        public void UploadAccelData(float x, float y, float z)
        {
            AccelerationX = x;
            AccelerationY = y;
            AccelerationZ = z;

            /*debug code*/
            //Console.WriteLine("UploadAccelData: X:{0}, Y:{1}, Z:{2}", x, y, z);
        }

        /// <summary>
        /// Stores information on the server relative to the 
        /// Windows Phone 7 rotation 
        /// </summary>
        /// <param name="x">Rotation on the X axis</param>
        /// <param name="y">Rotation on the Y axis</param>
        /// <param name="z">Rotation on the Z axis</param>
        public void UploadRotationData(float x, float y, float z)
        {
            RotationX = x;
            RotationY = y;
            RotationZ = z;

            /*debug code*/
            //Console.WriteLine("UploadAccelData: X:{0}, Y:{1}, Z:{2}", x, y, z);
        }

        /// <summary>
        /// Stores information on the server indicatin that the Windows Phone 7
        /// XNA Accelerometer Simulator application has started
        /// </summary>
        public void NotifyXNAStarted()
        {
            XNAStarted = true;
        }

        /// <summary>
        /// Retrieves information about the status of the Windows phone 7
        /// XNA Accelerometer Simulator application
        /// </summary>
        /// <returns>
        /// True if the application is running
        /// False otherwise
        /// </returns>
        public bool GetXNAStatus()
        {
            return XNAStarted;
        }
        #endregion
    }
}
